package org.zhxy.zhxy.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.zhxy.zhxy.entity.Admin;
import org.zhxy.zhxy.dao.AdminDao;
import org.zhxy.zhxy.service.AdminService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @ClassName AdminServiceImpl
 * @Description 管理员表(Admin)表服务实现类
 * @author yayong
 * @date 2024-10-19 20:29:59
 * @Version 1.0
 **/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminDao, Admin>  implements AdminService {

    @Autowired
    private AdminDao adminDao;

    @Override
    public Admin getByUsernameAndPwd(String username, String encodePwd) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("password",encodePwd);
        return adminDao.selectOne(queryWrapper);
    }

    @Override
    public int forgetPwd(String email, String encodePwd) {
        Admin admin = getByEmail(email);
        if(ObjectUtil.isNull(admin)){
            return 0;
        }
        int id = admin.getId();
        Admin forUpdate = new Admin();
        forUpdate.setId(id);
        forUpdate.setPassword(encodePwd);
        return adminDao.updateById(forUpdate);
    }

    @Override
    public Admin getByEmail(String email) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",email);
        return adminDao.selectOne(queryWrapper);
    }

    @Override
    public boolean usernameExists(String username) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username); // 根据用户名条件查询
        return this.count(queryWrapper) > 0; // 如果返回的数量大于 0，表示存在
    }
}

